package com.briup.jz.service.impl;

import com.briup.jz.bean.AccountCustomer;
import com.briup.jz.bean.AccountCustomerExample;
import com.briup.jz.bean.extend.AccountCustomerExtend;
import com.briup.jz.dao.AccountCustomerMapper;
import com.briup.jz.dao.extend.AccountApplyExtendMapper;
import com.briup.jz.dao.extend.AccountCustomerExtendMapper;
import com.briup.jz.service.IAccountCustomerService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class AccountCustomerimpl implements IAccountCustomerService {
    @Autowired
    private AccountCustomerMapper accountCustomerMapper;
    @Autowired
    private AccountCustomerExtendMapper accountCustomerExtendMapper;
//    添加或者更新用户消费信息
    @Override
    public void saveOrUpdate(AccountCustomer accountCustomer) throws CustomerException {
        accountCustomer.setTransferTime(new Date().getTime());
//        如果顾客id为空添加顾客信息
        if(accountCustomer.getId()==null){
            accountCustomerMapper.insert(accountCustomer);
        }else {
//            修改顾客信息
//            accountCustomerMapper.updateByPrimaryKey(accountCustomer);
            throw  new CustomerException("没有该消费信息");
        }
    }
//根据顾客id查询消费信息
    @Override
    public List<AccountCustomer> query(Long id) {
        AccountCustomerExample example=new AccountCustomerExample();
        //            如果id不为空则按id查询 否则则查询所有
        if (id != null) {
            example.createCriteria().andUserIdEqualTo(id);
        }

        return accountCustomerMapper.selectByExample(example);
    }

//    根据id删除消费记录
    @Override
    public void deleteById(Long id) throws CustomerException {
//查询是否有该信息
        AccountCustomer accountCustomer=accountCustomerMapper.selectByPrimaryKey(id);
        if (accountCustomer==null){
            throw new CustomerException("你删除的顾客消费信息不存在");
        }
//        删除顾客信息
        accountCustomerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<AccountCustomerExtend> queryWithUser(Long id) {
        List<AccountCustomerExtend> accountCustomerExtends=new ArrayList<>();
        if (id==null){
            accountCustomerExtends=accountCustomerExtendMapper.selectAllCustomer();
        }else {
accountCustomerExtends.add( accountCustomerExtendMapper.selectCustomerWithUser(id))  ;
        }
        return accountCustomerExtends;
    }
}
